<template>
  <div >

        <el-form :model="queryParams" ref="queryForm" :inline="true"  label-width="68px">
          <el-form-item label="角色名称" prop="roleName">
            <el-input
              v-model="queryParams.roleName"
              placeholder="模糊查询"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="selectRoleIndicator"
            />
          </el-form-item>
          <el-form-item label="指标名称" prop="indicatorName">
            <el-input
              v-model="queryParams.indicatorName"
              placeholder="模糊查询"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="selectRoleIndicator"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="selectRoleIndicator">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>


        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="clickAdd"
            >添加角色指标</el-button>
          </el-col>
        </el-row>
        <el-table v-loading="loading" :data="roleIndicatorList" :header-cell-style="getRowClass">
          <el-table-column label="编号"  align="center" key="id" prop="id" v-if="columns[0].visible" />
          <el-table-column label="角色ID" align="center" key="rid" prop="rid" v-if="columns[1].visible" :show-overflow-tooltip="true" />
          <el-table-column label="角色名字" align="center" key="roleName" prop="roleName" v-if="columns[1].visible" :show-overflow-tooltip="true" />
          <el-table-column label="根指标ID" align="center" key="iid" prop="iid" v-if="columns[2].visible" :show-overflow-tooltip="true" />
          <el-table-column label="指标名字" align="center" key="indicatorName" prop="indicatorName" v-if="columns[4].visible" :show-overflow-tooltip="true" />
          <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[8].visible" width="160">
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="200"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-s-order"
                @click="handleQuestionnaire(scope.row)"
              >问卷</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除</el-button>

            </template>
          </el-table-column>
        </el-table>


        <pagination
          v-show="this.sum>0"
          :total="this.sum"
          :page.sync='queryParams.page'
          :limit.sync='queryParams.number'
          @pagination="selectRoleIndicator"
        />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="角色">
              <el-select v-model="form.rid" placeholder="请选择">
                <el-option
                  v-for="dict in roleOptions"
                  :key="dict.id"
                  :label="dict.format"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="指标">
              <el-select v-model="form.iid" placeholder="请选择">
                <el-option
                  v-for="dict in indicatorOptions"
                  :key="dict.id"
                  :label="dict.format"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="titleQuestionnaire" :visible.sync="openQuestionnaire" width="1000px" append-to-body>
      <template v-for='questionnaire in questionnaireList' >
        <div :key="questionnaire.id">
          <div class="mainTitle">
            <span  class="title">{{ questionnaire.name }}</span>
         </div>
         <questionnaire :radio="radio" :hierarchy="hierarchy" :questionnaireList = "questionnaire.children" v-if="questionnaire.children!=null && questionnaire.children.length>0"></questionnaire>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {getRolesApi,getIndicatorsApi} from "@/api/system/common"
import {selectRoleIndicatorApi,deleteRoleIndicatorApi,addRoleIndicatorApi,updateRoleIndicatorApi} from "@/api/system/indicator/role"
import {selectQuestionnaireIndicatorOptionsApi} from "@/api/system/indicator/info"
import questionnaire from '@/components/questionnaire'
export default {
  name: "user",
  components: {
    questionnaire
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 用户表格数据
      roleIndicatorList: [],
      //问卷属性
      questionnaireList:[],
      //当前层
      hierarchy:0,
      radio:[],
      sum: 0,
       // 弹出层标题
      title: "",
      titleQuestionnaire:"",
      // 是否显示弹出层
      open: false,
      openQuestionnaire: false,
      // 角色数据
      roleOptions: [],
      // 指标数据
      indicatorOptions: [],
      // 日期范围
      dateRange: [],
      form: {},
      // 查询参数
      queryParams: {
        page: 1,
        number: 10,
        roleName: undefined,
        indicatorName: undefined
      },
      // 列信息
      columns: [
        { key: 0, label: `用户编号`, visible: true },
        { key: 1, label: `用户名称`, visible: true },
        { key: 2, label: `登录账号`, visible: true },
        { key: 3, label: `部门名称`, visible: true },
        { key: 4, label: `性别`, visible: true },
        { key: 5, label: `手机号码`, visible: true },
        { key: 6, label: `邮箱`, visible: true },
        { key: 7, label: `状态`, visible: true },
        { key: 8, label: `创建时间`, visible: true }
      ],
      // 表单校验
      rules: {
        account: [
          { required: true, message: "用户名称不能为空", trigger: "blur" },
          { min: 2, max: 20, message: '用户名称长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        name: [
          { required: true, message: "登录不能为空", trigger: "blur" }
        ],
        email: [
          {
            type: "email",
            message: "'请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        mobile: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  watch: {

  },
  created() {
    getRolesApi().then(data=>{
      this.roleOptions = data.data;
    });
    getIndicatorsApi().then(data=>{
      this.indicatorOptions = data.data;
    });
    this.selectRoleIndicator();
  },
  methods: {
    getRowClass ({ rowIndex }) {
      if (rowIndex == 0) {
        return 'background:#afdfe4'
      } else {
        return ''
      }
    },
    //查询
    selectRoleIndicator(){
     selectRoleIndicatorApi(this.queryParams).then(data=>{
        this.roleIndicatorList = data.data.returnDate;
        this.sum = data.data.paginationData.sum;
        this.loading = false;
      }).catch(()=>{
        this.roleIndicatorList = null;
        this.sum = 0;
        this.loading = false;
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      if (this.$refs["queryForm"]) {
		    this.$refs["queryForm"].resetFields();
	    }
      this.selectRoleIndicator();
    },
    // 用户状态修改
    statusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$confirm('确认要"' + text + '""' + row.name + '"用户吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(()=> {
         updateUserStatusApi({
            "id": row.id,
            "status": row.status
          }).then(()=>{
            this.$message({
               type: 'success',
              message: '修改成功'
            });
          })
        }).catch(()=> {
          row.status = row.status === "0" ? "1" : "0";
        });
    },
    /** 问卷按钮操作 */
    handleQuestionnaire(row) {
      selectQuestionnaireIndicatorOptionsApi({
        "id": row.iid,
        "isQuestionnaire":true,
        "page":1,
        "number":1
      }).then(data=>{
       this.questionnaireList = data.data;
       if(this.questionnaireList.length>0){
         this.titleQuestionnaire = this.questionnaireList[0].name;
       }
       console.log(this.questionnaireList)
       this.openQuestionnaire = true;
      }).catch(()=>{
         this.questionnaireList = [];
         this.openQuestionnaire = true;
      })

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除角色指标编号为"' + row.id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(()=> {
          deleteRoleIndicatorApi({
            "id": row.id
          }).then(data=>{
            this.selectRoleIndicator();
            this.$message({
               type: 'success',
              message: '删除成功'
            });
          })
        }).catch(() => {

        });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      selectRoleIndicatorApi({
        "id": row.id,
        "page":1,
        "number":1
      }).then(data=>{
        this.title = "修改";
        this.form.id = data.data.returnDate[0].id;
        this.form.iid = data.data.returnDate[0].iid;
        this.form.rid = data.data.returnDate[0].rid;
        this.open = true;
      })
    }, // 表单重置
    reset() {
      this.form = {
        id: undefined,
        iid:undefined,
        rid:undefined
      };
       if (this.$refs["form"]) {
		    this.$refs["form"].resetFields();
	    }
    },
    clickAdd(){
      this.title = "添加";
      this.open = true;
      this.reset();
    },
     /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateRoleIndicatorApi(this.form).then(data=>{
              this.open = false;
              this.selectRoleIndicator();
              this.reset();
              this.$message({
                type: 'success',
                message: '修改成功'
              });
            })
          } else {
            addRoleIndicatorApi(this.form).then(data=>{
              this.open = false;
              this.selectRoleIndicator();
              this.reset();
              this.$message({
                type: 'success',
                message: '添加成功'
              });
            })
          }
        }
      });
    }, // 取消按钮
    cancel() {
      this.open = false;
    }
  }
};
</script>


<style scoped>
  .mainTitle{
    display:flex;
  }
  .title{
    margin:10px auto;

    font-size: 40px;
  }
</style>
